package com.ty.init.controller;

import com.ty.init.pojo.Product;
import com.ty.init.pojo.User;
import com.ty.init.service.ProductService;
import com.ty.init.service.UserService;
import com.ty.init.uitl.Vo.IncomeVo;
import com.ty.init.uitl.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ty
 */
@Controller
public class ProductController extends BaseController{
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    @RequestMapping("product")
    public String productList(){
        return "product/list";
    }
    @ResponseBody
    @RequestMapping("productlist")
    public JsonResult<List<Product>> findProductPage(Integer page , Integer limit){
        Integer count = productService.findAllProduct();
        int start = (page - 1)*limit;
        List<Product> data = productService.finProductLimit(start, limit);
        return new JsonResult<>(OK,count,data);
    }
    @ResponseBody
    @RequestMapping("messageList")
    public JsonResult<Product> MessageList(Integer id){
        Product data = productService.findByID(id);
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("userUpdate")
    public String productUpdate(Model model , Integer uid){
        User data = userService.findById(uid);
        model.addAttribute("data",data);
        System.out.println(uid);
        return "user/userUpdate";
    }
    @RequestMapping("OwnerUpdate")
    public String ownerUpdate(HttpSession session ,Model model,Integer uid){
        User data = userService.findById(getUidFromSession(session));
        model.addAttribute("data",data);
        System.out.println(uid);
        return "user/ownerUpdate";
    }
    @RequestMapping("productIncome")
    public String productIncome(){
        return "product/incomeMessage";
    }
    @RequestMapping("incomeMessage")
    @ResponseBody
    public JsonResult<List<IncomeVo>> incomeMessage(){
        List<IncomeVo> data = productService.incomeMessage();
        return new JsonResult<>(OK,data);
    }
}
